package com.acoreful.acf2java.admin.modules.sys.web;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.acoreful.acf2java.admin.config.AdminConfig;
import com.acoreful.acf2java.admin.modules.auth.support.CurrentStaff;
import com.acoreful.acf2java.admin.modules.auth.support.annotation.Subject;

/**
 * 访问项目根路径时的处理
 * 
 */
@RequestMapping
@Controller
public class IndexController {

	@Autowired
	private AdminConfig adminConfig;

	@GetMapping("/")
	public String dispatchIndex(@Subject(required = false) CurrentStaff currentStaff) {
		if (currentStaff != null) { // 已登录
			return "redirect:" + adminConfig.getAppUrlIndex();
		}
		return "redirect:" + adminConfig.getAppUrlLogin(); // 未登录
	}

	@GetMapping("/index")
	public String index(@Subject CurrentStaff currentStaff, Model model) {
		model.addAttribute("menus", null);
		model.addAttribute("permissions", "|" + StringUtils.join(currentStaff.getPermissions(), "|") + "|");
		model.addAttribute("username", currentStaff.getCname());
		return "index";
	}
	@GetMapping("/dashboard")
	public String dashboard(@Subject CurrentStaff currentStaff, Model model) {
		model.addAttribute("menus", null);
		model.addAttribute("permissions", "|" + StringUtils.join(currentStaff.getPermissions(), "|") + "|");
		model.addAttribute("username", currentStaff.getCname());
		return "dashboard";
	}
}
